import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Game {
	private Board board;
	private PlayerType player1;
	private int levelPlayer1;
	private PlayerType player2;
	private int levelPlayer2;
	private Engine engine;
//	private int DEPTH = 2;

	public Game(PlayerType p1, int levelp1, PlayerType p2, int levelp2) {
		board = new Board();
		board.InitBoard();
		player1 = p1;
		levelPlayer1=levelp1;
		player2 = p2;
		levelPlayer2 = levelp2;
		engine = new Engine();
	}

	public void StartGame() {
		while (!board.isFull()) {
			board.Display();
			if (board.getCurrentPlayer() == colorCode.WHITE) {
				Play(player1, board, levelPlayer1);
			} else {
				Play(player2, board, levelPlayer2);
			}
		}
		System.out.println("TERMINE!");
		board.Display();
	}

	private void Play(PlayerType pt, Board b, int level) {
		if (pt == PlayerType.HUMAN) {
			PlayAsHuman(b);
		} else // pt == PlayerType.MACHINE
		{
			PlayAsMachine(b, level);
		}

	}

	private void PlayAsMachine(Board b, int level) {
		Move m = engine.getBestMove(b,level);
		b.Play(m);
	}

	private void PlayAsHuman(Board b) {

		// open up standard input
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		int x;
		int y;
		try {
			x = Integer.parseInt(br.readLine());
			y = Integer.parseInt(br.readLine());
			b.Play(new Move(x, y));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
